package com.zmm.weblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zmm.weblog.common.config.InsertBatchMapper;
import com.zmm.weblog.common.domain.dos.ArticleTagRelDo;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
@Mapper
public interface ArticleTagRelMapper extends InsertBatchMapper<ArticleTagRelDo> {

    default int deleteByArticleId(Long articleId){
        return delete(Wrappers.<ArticleTagRelDo>lambdaQuery()
                .eq(ArticleTagRelDo::getArticleId, articleId));
    }

    /**
     * 根据文章ID来查询
     * @param articleId
     * @return
     */
    default List<ArticleTagRelDo> selectByArticleId(Long articleId){
        return selectList(Wrappers.<ArticleTagRelDo>lambdaQuery()
                .eq(ArticleTagRelDo::getArticleId, articleId));
    }

    /**
     * 根据标签ID来查询
     * @param tagId
     * @return
     */
    default ArticleTagRelDo selectByTagId(Long tagId) {
        return selectOne(Wrappers.<ArticleTagRelDo>lambdaQuery()
                .eq(ArticleTagRelDo::getTagId, tagId)
                .last("limit 1"));
    }

    default List<ArticleTagRelDo> selectBatchByArticleIds(List<Long> articleIds){
        return selectList(Wrappers.<ArticleTagRelDo>lambdaQuery().in(ArticleTagRelDo::getArticleId, articleIds));
    }

    /**
     * 查询该标签ID下所有关联记录
     * @param tagId
     * @return
     */
    default List<ArticleTagRelDo> selectListByTagId(Long tagId){
        return selectList(Wrappers.<ArticleTagRelDo>lambdaQuery().eq(ArticleTagRelDo::getTagId, tagId));
    }
}